﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Paltop_DatabaseLayer;
using System;
using System.Net;
using System.Net.Mail;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Paltop_DatabaseLayer;
using System.Text;
using System.Security.Cryptography;
using System.Text.RegularExpressions;
using Paltop_v0._1.Entities;

namespace Paltop_.Controllers
{
    public class UserlogController
    {
        private PaltopDatabaseEntities db = new PaltopDatabaseEntities();
        private List<Os> OSNames = (List<Os>)HttpContext.Current.Application["osTypes"];
        private List<Browser> BrowserNames = (List<Browser>)HttpContext.Current.Application["browserTypes"];
        private List<Resolution> ResolutionNames = (List<Resolution>)HttpContext.Current.Application["resTypes"];
        public Dictionary<int, int> osCounter()
        {
            Dictionary<int, int> oslist = new Dictionary<int, int>();
            foreach (var item in OSNames)
            {
                var t = db.SYSUserLogs.Where(p => p.OSID == item.OSID).ToList();
                oslist[item.OSID] = t.Count;
            }
            return oslist;
        }
        public Dictionary<int, int> browserCounter()
        {
            Dictionary<int, int> browserlist = new Dictionary<int, int>();
            foreach (var item in BrowserNames)
            {
                var t = db.SYSUserLogs.Where(p => p.BrowserID == item.BrowserID).ToList();
                browserlist[item.BrowserID] = t.Count;
            }
            return browserlist;
        }
        public Dictionary<int, int> resolutionCounter()
        {
            Dictionary<int, int> resolutionlist = new Dictionary<int, int>();
            foreach (var item in ResolutionNames)
            {
                var t = db.SYSUserLogs.Where(p => p.ResolutionID == item.ResolutionID).ToList();
                resolutionlist[item.ResolutionID] = t.Count;
            }
            return resolutionlist;
        }
        public int TodayVisit()
        {
            DateTime today = DateTime.Today;
            var t = db.SYSUserLogs.Select(p => p).ToList();
            int counter = 0;
            foreach (var item in t)
            {
                if (item.Date.Date == today.Date)
                    counter++;
            }
            return counter;
        }

        public int YesterDayVisit()
        {
            DateTime today = DateTime.Today;
            today = today.AddDays(-1);
            var t = db.SYSUserLogs.Select(p => p).ToList();
            int counter = 0;
            foreach (var item in t)
            {
                if (item.Date.Date == today.Date)
                    counter++;
            }
            return counter;
        }

        public int TotalVisitors()
        {
            return db.SYSUserLogs.Count();
        }

        public Int32[] thisMounthVisit()
        {
            DateTime today = DateTime.Today;
            Int32[] a = new Int32[31];
            for (int i = 0; i < a.Length; i++)
            {
                a[i] = 0;
            }
            var t = db.SYSUserLogs.Select(p => p).ToList();
            foreach (var item in t)
            {
                if (item.Date.Month == today.Month)
                    a[item.Date.Day]++;
            }
            return a;
        }

        public void GenerateDate()
        {
            osCounter();
            browserCounter();
            TodayVisit();
            resolutionCounter();


        }
    }
}